package com.hmall.cart.config;

import feign.*;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.concurrent.TimeUnit;

/**
 * @author Mr.M
 * @version 1.0
 * @description okhttp连接池配置
 * @date 2024/8/3 17:43
 */
@Configuration
public class FeignClientConfiguration {

    @Value("${okhttp.max-connections:1}")
    private int maxConnections;

    @Value("${okhttp.connect-timeout:10}")
    private int connectTimeout;

    @Value("${okhttp.read-timeout:20}")
    private int readTimeout;

    @Value("${okhttp.write-timeout:30}")
    private int writeTimeout;

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                .readTimeout(readTimeout, TimeUnit.SECONDS)
                .writeTimeout(writeTimeout, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(maxConnections, 10, TimeUnit.MINUTES))
                .build();
    }

//    @Bean
//    public Client feignClient(OkHttpClient okHttpClient) {
//        return new feign.okhttp.OkHttpClient(okHttpClient);
//    }
//    @Bean
//    public Feign.Builder feignBuilder(OkHttpClient okHttpClient) {
//        return Feign.builder().client(new feign.okhttp.OkHttpClient(okHttpClient));
//    }
    @Bean
    public Retryer myRetryer()
    {
        return Retryer.NEVER_RETRY; //Feign默认配置是不走重试策略的
        //最大请求次数为3(1+2)，初始间隔时间为1s，重试间最大间隔时间为3s
//        return new Retryer.Default(1000,3000,3);
    }
//    @Bean
//    public Logger.Level feignLogLevel(){
//        return Logger.Level.FULL;
//    }


}
